//
//  RZVideoMessageCell.m
//  RZIM
//
//  Created by reyzhang on 2021/8/26.
//  视频消息

#import "RZVideoMessageCell.h"
#import <SDWebImage/SDWebImage.h>

@interface RZVideoMessageCell ()
@property (nonatomic, strong) UIView *playButtonView;
@property (nonatomic, strong) UILabel *durationLabel;
@property (nonatomic, strong) UIImageView *playImage;
@end


@implementation RZVideoMessageCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Super Methods

+ (CGSize)sizeForMessageModel:(RZMessageInfo *)model
      withCollectionViewWidth:(CGFloat)collectionViewWidth
         referenceExtraHeight:(CGFloat)extraHeight {
    CGFloat messagecontentview_height = 0.0f;
    messagecontentview_height = [self getSightImageSize:model].height;
    if (messagecontentview_height < RZIMConfigCenter.ui.globalMessagePortraitSize.height) {
        messagecontentview_height = RZIMConfigCenter.ui.globalMessagePortraitSize.height;
    }
    messagecontentview_height += extraHeight;
    return CGSizeMake(collectionViewWidth, messagecontentview_height);
}

- (void)setDataModel:(RZMessageInfo *)model {
    [super setDataModel:model];
    self.thumbnailView.image = nil;
    RZVideoMessageModel *sightMessage = (RZVideoMessageModel *)model.extraInfo;
    if (sightMessage) {
        CGSize imageSize = [RZVideoMessageCell getSightImageSize:self.model];
        self.durationLabel.text = [self getSightDurationLabelText:sightMessage.duration];
        [self.thumbnailView sd_setImageWithURL:[NSURL URLWithString:sightMessage.imageUrl] placeholderImage:sightMessage.thumbImage options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            sightMessage.thumbImage = image;
        }];

        self.messageContentView.contentSize = imageSize;
        self.thumbnailView.frame = self.messageContentView.bounds;
        
        
        [self.playImage setCenter:CGPointMake(self.thumbnailView.bounds.size.width / 2,
                                              self.thumbnailView.bounds.size.height / 2)];
        
        CGRect durationLabelBgFrame =
            CGRectMake(0, self.thumbnailView.bounds.size.height - 21, self.thumbnailView.bounds.size.width, 21);
        self.durationLabel.superview.frame = durationLabelBgFrame;
        self.durationLabel.frame =
            CGRectMake(0, 0, durationLabelBgFrame.size.width-5, durationLabelBgFrame.size.height);
    } else {
        NSLog(@"video info is empty");
    }
    [self updateSightPlayStatus];
}

- (void)updateSightPlayStatus{
    [self.playButtonView setHidden:NO];
}

#pragma mark - Private Methods
+ (CGSize)getSightImageSize:(RZMessageInfo *)model{
    RZVideoMessageModel *sightMessage = (RZVideoMessageModel *)model.extraInfo;

    CGSize imageSize = (CGSize){sightMessage.width,sightMessage.height};
    //兼容240
    CGFloat rate = imageSize.width / imageSize.height;
    CGFloat imageWidth = 0;
    CGFloat imageHeight = 0;

    if (imageSize.width != 0 && imageSize.height != 0) {
        if (rate > 1.0f) {
            imageWidth = 160;
            imageHeight = 160 / rate;
        } else {
            imageHeight = 160;
            imageWidth = 160 * rate;
        }
    } else {
        imageWidth = imageSize.width;
        imageHeight = imageSize.height;
    }
    return CGSizeMake(imageWidth, imageHeight);
}

- (void)initialize {
    [self.messageContentView addSubview:self.thumbnailView];
}

- (NSString *)getSightDurationLabelText:(long)duration{
    NSInteger minutes = duration / 60;
    NSInteger seconds = round(duration - minutes * 60);
    if (seconds == 60) {
        minutes += 1;
        seconds = 0;
    }
    return [NSString stringWithFormat:@"%02ld:%02ld", (long)minutes, (long)seconds];
}

#pragma mark - Getters and Setters

- (UILabel *)durationLabel {
    if (!_durationLabel) {
        _durationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 21)];
        [_durationLabel setTextAlignment:NSTextAlignmentRight];
        [_durationLabel setBackgroundColor:[UIColor clearColor]];
        [_durationLabel setTextColor:[UIColor whiteColor]];
        [_durationLabel setFont:[RZIMConfigCenter.font fontOfAnnotationLevel]];
    }
    return _durationLabel;
}

- (UIImageView *)playImage {
    if (!_playImage) {
        _playImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 41, 41)];
        UIImage *image = RZResourceImage(@"sight_message_icon");
        _playImage.image = image;
    }
    return _playImage;
}


//在getter时被添加到了视图中
- (UIView *)playButtonView {
    if (!_playButtonView) {
        _playButtonView = [[UIView alloc] initWithFrame:self.thumbnailView.bounds];
        [_playButtonView addSubview:self.playImage];
        [self.thumbnailView addSubview:_playButtonView]; //将播放按钮添加占位图上
        
        
        UIImageView *backgroudView =
            [[UIImageView alloc] initWithFrame:CGRectMake(0, self.thumbnailView.bounds.size.height - 21,
                                                          self.thumbnailView.bounds.size.width, 21)];
        backgroudView.image = RZResourceImage(@"player_shadow_bottom");
        [_playButtonView addSubview:backgroudView];
        [backgroudView addSubview:self.durationLabel];
    }
    return _playButtonView;
}

- (UIImageView *)thumbnailView{
    if (!_thumbnailView) {
        _thumbnailView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _thumbnailView.layer.masksToBounds = YES;
        _thumbnailView.layer.cornerRadius = 6;
        _thumbnailView.backgroundColor  = RGBCOLOR(220, 237, 251);
    }
    return _thumbnailView;
}


@end
